//
//  MyfileViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "MyfileViewController.h"
#import "LoginViewController.h"
#import "MyactivityViewController.h"
#import "MymovieViewController.h"
#import "DatabaseSingleton.h"


@interface MyfileViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView * myTableView;
@property (nonatomic, retain) NSMutableArray *myArray;

@end

@implementation MyfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-68) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myfile"];
    self.myArray = [NSMutableArray arrayWithObjects:@"我的活动",@"我的电影",@"清除缓存", nil];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 40);
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    NSString *a = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];
    if ([a isEqualToString:@"1"]) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 40);
        [button addTarget:self action:@selector(butAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"注销" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
        
    }
    
    
}
-(void)butAction:(UIButton *)but{
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        
        
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"1"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 40);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"登录" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
        
        
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    

    
}

-(void)buttonAction:(UIButton *)but{
    LoginViewController *loginVIew = [[LoginViewController alloc] init];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:loginVIew];
    [self presentViewController:navC animated:YES completion:^{
        
        
    }];
    [loginVIew release];
    [navC release];

    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndent = @"myfile";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
    return cell;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *a = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];
    if ([a isEqualToString:@"1"]) {
        if (indexPath.row == 0) {
            
            MyactivityViewController *myActi = [[MyactivityViewController alloc] init];
            [self.navigationController pushViewController:myActi animated:YES];
        }
        if (indexPath.row == 1) {
            
            MymovieViewController *myMovie = [[MymovieViewController alloc] init];
            [self.navigationController pushViewController:myMovie animated:YES];
        }
        if (indexPath.row == 2){
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清理" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                //
                [[DatabaseSingleton shareDatabaseSingleton] dropTable];
                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
                
            }];
            
            [alert addAction:action1];
            [alert addAction:action2];
            
            
            
        }

        
    }else{
        
        LoginViewController *loginVIew = [[LoginViewController alloc] init];
        UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:loginVIew];
        [self presentViewController:navC animated:YES completion:^{
            
            
        }];
        [loginVIew release];
        [navC release];

    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
